package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.ITransferAccountsZfbSafetyBatchDAO;
import org.jsola.hr.dto.TransferAccountsZfbSafetyBatchAddDTO;
import org.jsola.hr.dto.TransferAccountsZfbSafetyBatchUpdateDTO;
import org.jsola.hr.entity.TransferAccountsZfbSafetyBatchDO;
import org.jsola.hr.query.TransferAccountsZfbSafetyBatchQuery;
import org.jsola.hr.service.ITransferAccountsZfbSafetyBatchService;
import org.jsola.hr.vo.TransferAccountsZfbSafetyBatchListVO;
import org.jsola.hr.vo.TransferAccountsZfbSafetyBatchVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 转账批次(支付宝安全发)
 *
 * @author zhr
 */
@Service("hrTransferAccountsZfbSafetyBatchServiceImpl")
public class TransferAccountsZfbSafetyBatchServiceImpl implements ITransferAccountsZfbSafetyBatchService {

    @Autowired
    private ITransferAccountsZfbSafetyBatchDAO transferAccountsZfbSafetyBatchDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public TransferAccountsZfbSafetyBatchVO save(TransferAccountsZfbSafetyBatchAddDTO transferAccountsZfbSafetyBatchAddDTO, TokenUser tokenUser) {
        // 转DO
        TransferAccountsZfbSafetyBatchDO transferAccountsZfbSafetyBatchDO = transferAccountsZfbSafetyBatchAddDTO.to(TransferAccountsZfbSafetyBatchDO.class);
        // 保存
        transferAccountsZfbSafetyBatchDO = save(transferAccountsZfbSafetyBatchDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return transferAccountsZfbSafetyBatchDO.to(TransferAccountsZfbSafetyBatchVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(TransferAccountsZfbSafetyBatchUpdateDTO transferAccountsZfbSafetyBatchUpdateDTO, TokenUser tokenUser) {
        //转DO
        TransferAccountsZfbSafetyBatchDO transferAccountsZfbSafetyBatchDO = transferAccountsZfbSafetyBatchUpdateDTO.to(TransferAccountsZfbSafetyBatchDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(transferAccountsZfbSafetyBatchDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... transferAccountsZfbSafetyBatchIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) transferAccountsZfbSafetyBatchIds);
    }


    @Override
    public TransferAccountsZfbSafetyBatchVO selectById(String transferAccountsZfbSafetyBatchId, String siteId) {
        TransferAccountsZfbSafetyBatchDO transferAccountsZfbSafetyBatchDO = selectDOById(transferAccountsZfbSafetyBatchId, siteId);
        if (transferAccountsZfbSafetyBatchDO == null) {
            return null;
        }
        return transferAccountsZfbSafetyBatchDO.to(TransferAccountsZfbSafetyBatchVO.class);
    }

    @Override
    public List<TransferAccountsZfbSafetyBatchListVO> select(TransferAccountsZfbSafetyBatchQuery transferAccountsZfbSafetyBatchQuery, String siteId) {
        List<TransferAccountsZfbSafetyBatchDO> transferAccountsZfbSafetyBatchDOList = selectDO(transferAccountsZfbSafetyBatchQuery, siteId);
        if(CollectionUtils.isEmpty(transferAccountsZfbSafetyBatchDOList)) {
            return transferAccountsZfbSafetyBatchDOList == null ? null : new ArrayList<>();
        }
        return transferAccountsZfbSafetyBatchDOList.stream()
                .map(transferAccountsZfbSafetyBatchDO -> transferAccountsZfbSafetyBatchDO.to(TransferAccountsZfbSafetyBatchListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(TransferAccountsZfbSafetyBatchQuery transferAccountsZfbSafetyBatchQuery, String siteId) {
        Example example = buildExample(transferAccountsZfbSafetyBatchQuery,siteId);
        return transferAccountsZfbSafetyBatchDAO.selectCountByExample(example);
    }

    @Override
    public Page<TransferAccountsZfbSafetyBatchListVO> selectPage(TransferAccountsZfbSafetyBatchQuery transferAccountsZfbSafetyBatchQuery, String siteId) {
        Example example = buildExample(transferAccountsZfbSafetyBatchQuery,siteId);
        Page<TransferAccountsZfbSafetyBatchDO> page = transferAccountsZfbSafetyBatchDAO.selectPageByExample(example,
                transferAccountsZfbSafetyBatchQuery.getPageNo(),
                transferAccountsZfbSafetyBatchQuery.getPageSize());

        return page.to(TransferAccountsZfbSafetyBatchListVO.class);
    }

    @Override
    public TransferAccountsZfbSafetyBatchDO selectDOById(String transferAccountsZfbSafetyBatchId, String siteId) {
        return listById(transferAccountsZfbSafetyBatchId, siteId);
    }

    @Override
    public TransferAccountsZfbSafetyBatchDO selectDOById(String transferAccountsZfbSafetyBatchId) {
        Example example = new Example(TransferAccountsZfbSafetyBatchDO.class);
        example.and()
                .andEqualTo("id", transferAccountsZfbSafetyBatchId)
                .andEqualTo("valid", true);
        return transferAccountsZfbSafetyBatchDAO.selectOneByExample(example);
    }

    @Override
    public List<TransferAccountsZfbSafetyBatchDO> selectDO(TransferAccountsZfbSafetyBatchQuery transferAccountsZfbSafetyBatchQuery, String siteId) {
        Example example = buildExample(transferAccountsZfbSafetyBatchQuery,siteId);
        return transferAccountsZfbSafetyBatchDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param transferAccountsZfbSafetyBatchQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(TransferAccountsZfbSafetyBatchQuery transferAccountsZfbSafetyBatchQuery, String siteId) {
        Example example = new Example(TransferAccountsZfbSafetyBatchDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (transferAccountsZfbSafetyBatchQuery.getPayerBookId() != null) {
            example.and().andEqualTo("payerBookId", transferAccountsZfbSafetyBatchQuery.getPayerBookId());
        }
        // 排序
        ExampleKit.setExampleOrder(example,transferAccountsZfbSafetyBatchQuery.getOrders());
        return example;
    }
}




